package com.cneiu.ppms.web.service;

import java.util.Map;

import net.sf.json.JSONObject;

import com.cneiu.ppms.web.base.BaseService;
import com.cneiu.ppms.web.model.JobModel;
import com.jfinal.plugin.activerecord.Page;

/**
 * 岗位业务
 * @author zhaokf
 * @date 2013-04-15
 */
public class JobService extends BaseService
{
	/**
	 * 分页列出岗位
	 * @param page
	 * @param limit
	 * @param search
	 * @return
	 */
	public static Page<JobModel> list(Map<String,String> params){
		JSONObject search = JSONObject.fromObject(params.get("search"));
		int limit = 0;
		int page = 1;
		try{
			limit = params.get("limit")==null?0:Integer.parseInt(params.get("limit").toString());
			page = params.get("page")==null?1:Integer.parseInt(params.get("page").toString());
		}catch (Exception e){
			limit = 100;
			page = 1;
		}
		String where = "";
		try{
			if (search.get("job_no") != null){
				where = " AND job_no=" + search.get("job_no");
			}
			if (search.get("job_name") != null){
				where = " AND job_name LIKE '%" + search.get("job_name") + "%'";
			}
			where = " WHERE " + where.substring(5);
		}catch (Exception e){
		}
		Page<JobModel> Jobs = null;

		String select_sql = "SELECT * ";
		String query_sql = "FROM SA_JOB " + where;

		try{
			Jobs = JobModel.dao.paginate(page, limit, select_sql, query_sql);
		}catch (Exception e){
			setError(e.getMessage());
		}
		return Jobs;
	}



	/**
	 * 增加岗位信息
	 * @param no
	 * @param name
	 * @param type
	 * @return
	 */
	public static boolean isAdd(Map<String,String> params){
		
		String no = params.get("job_no")==null?"":params.get("job_no").toString();
		String name = params.get("job_name")==null?"":params.get("job_name").toString();
		String type = params.get("job_type")==null?"":params.get("job_type").toString();
		
		String sql = "select * from sa_job where job_no=?";
		JobModel jm = JobModel.dao.findFirst(sql, no);
		if (jm == null){
			jm =new JobModel();
			jm.set("job_no",no);
			jm.set("job_name", name);
			jm.set("job_org_type", type);
			jm.save();
			return true;
		}
		return setError("你访问的岗位信息已存在！");
	}

	/**
	 * 修改岗位信息
	 * @param no
	 * @param name
	 * @param type
	 * @return
	 */
	public static boolean isUpdate(Map<String,String> params){
		
		String no = params.get("job_no")==null?"":params.get("job_no").toString();
		String name = params.get("job_name")==null?"":params.get("job_name").toString();
		String type = params.get("job_type")==null?"":params.get("job_type").toString();
		
		JobModel jm = JobModel.dao.findById(no);
		if (jm != null){
			jm.set("job_no",no);
			jm.set("job_name", name);
			jm.set("job_org_type", type);
			jm.update();
			return true;
		}
		return setError("您访问的岗位信息不存在！");
	}

	/**
	 * 删除岗位信息
	 * @param no
	 * @return
	 */
	public static boolean isDelete(Map<String,String> params){
		String no = params.get("job_no")==null?"":params.get("job_no").toString();
		JobModel jm = JobModel.dao.findById(no);
		if (jm != null)
		{
			jm.delete();
			return true;
		}
		return setError("您访问的岗位信息不存在！");
	}
}
